package com.bbzn.user.client.service.impl;

import com.bbzn.user.client.dao.opthis.OptHisRepository;
import com.bbzn.user.client.dataobject.OptHis;
import com.bbzn.user.client.service.OptHisMongoService;
import com.bbzn.user.client.utils.BeanVOUtils;
import com.bbzn.user.client.utils.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OptHisMongoServiceImpl implements OptHisMongoService {

    @Autowired
    private OptHisRepository optHisRepository;

    @Override
    public OptHis findOne(Long id) {
        return optHisRepository.findById(id).orElseGet(null);
    }

    @Override
    public OptHis save(OptHis optHis) {
        return optHisRepository.save(optHis);
    }

    @Override
    public List<OptHis> queryAll() {
        return optHisRepository.findAll();
    }

    @Override
    public Page<OptHis> findAllByCreateTimeBetweenOrderByCreateTime(String startTime, String endTime,String imei,String createBy,Long userId, int currentPage, int pageSize) {
        Pageable pageableObj = PageRequest.of(currentPage,pageSize);
        Date startDate = DateUtil.stringToDate(startTime,"yyyy-MM-dd HH:mm:ss");
        Date endDate = DateUtil.stringToDate(endTime,"yyyy-MM-dd HH:mm:ss");
        if(BeanVOUtils.anyNotNull(startTime,endTime,imei,createBy)){
            return optHisRepository.findAllByCreateTimeBetweenOrderByCreateTime(startDate,endDate,imei,createBy,userId,pageableObj);
        }
        if(BeanVOUtils.anyNotNull(startTime,endTime,imei)){
            return optHisRepository.findAllByImeiBetweenOrderByCreateTime(startDate,endDate,imei,userId,pageableObj);
        }
        if(BeanVOUtils.anyNotNull(startTime,endTime,createBy)){
            return optHisRepository.findAllByCreateByBetweenOrderByCreateTime(startDate,endDate,createBy,userId,pageableObj);
        }
        if(BeanVOUtils.anyNotNull(startTime,endTime)){
            return optHisRepository.findAllByCreateTimeBetweenOrderByCreateTime(startDate,endDate,userId,pageableObj);
        }
        return optHisRepository.findAllByCreateTimeBetweenOrderByCreateTime(startDate,endDate,userId,pageableObj);
    }
}
